home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / DigestInputStream.java < prev    next >
Text File  |  1998-10-14  |  6KB  |  180 lines

  1. /*
  2.  * @(#)DigestInputStream.java    1.27 98/08/19
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. import java.io.IOException;
  18. import java.io.EOFException;
  19. import java.io.InputStream;
  20. import java.io.FilterInputStream;
  21. import java.io.PrintStream;
  22. import java.io.ByteArrayInputStream;
  23.  
  24. /** 
  25.  * A transparent stream that updates the associated message digest using 
  26.  * the bits going through the stream. 
  27.  *
  28.  * <p>To complete the message digest computation, call one of the 
  29.  * <code>digest</code> methods on the associated message 
  30.  * digest after your calls to one of this digest input stream's <a href = 
  31.  * "#read()">read</a> methods.
  32.  *      
  33.  * <p>It is possible to turn this stream on or off (see <a href =
  34.  * "#on">on</a>). When it is on, a call to <code>read</code>
  35.  * results in an update on the message digest.  But when it is off,
  36.  * the message digest is not updated. The default is for the stream
  37.  * to be on.
  38.  *
  39.  * <p>Note that digest objects can compute only one digest (see
  40.  * MessageDigest),
  41.  * so that in order to compute intermediate digests, a caller should
  42.  * retain a handle onto the digest object, and clone it for each
  43.  * digest to be computed, leaving the orginal digest untouched.
  44.  *
  45.  * @see MessageDigest
  46.  * 
  47.  * @see DigestOutputStream
  48.  *
  49.  * @version 1.27 98/10/05
  50.  * @author Benjamin Renaud 
  51.  */
  52.  
  53. public class DigestInputStream extends FilterInputStream {
  54.  
  55.     /* NOTE: This should be made a generic UpdaterInputStream */
  56.  
  57.     /* Are we on or off? */
  58.     private boolean on = true;
  59.  
  60.     /**
  61.      * The message digest associated with this stream.
  62.      */
  63.     protected MessageDigest digest;
  64.  
  65.     /**
  66.      * Creates a digest input stream, using the specified input stream
  67.      * and message digest.
  68.      *
  69.      * @param stream the input stream.
  70.      *
  71.      * @param digest the message digest to associate with this stream.
  72.      */
  73.     public DigestInputStream(InputStream stream, MessageDigest digest) {
  74.     super(stream);
  75.     setMessageDigest(digest);
  76.     }
  77.  
  78.     /**
  79.      * Returns the message digest associated with this stream.
  80.      *
  81.      * @return the message digest associated with this stream.
  82.      */
  83.     public MessageDigest getMessageDigest() {
  84.     return digest;
  85.     }    
  86.  
  87.     /**
  88.      * Associates the specified message digest with this stream.
  89.      *
  90.      * @param digest the message digest to be associated with this stream.  
  91.      */
  92.     public void setMessageDigest(MessageDigest digest) {
  93.     this.digest = digest;
  94.     }
  95.  
  96.     /**
  97.      * Reads a byte, and updates the message digest (if the digest
  98.      * function is on).  That is, this method reads a byte from the
  99.      * input stream, blocking until the byte is actually read. If the 
  100.      * digest function is on (see <a href = "#on">on</a>), this method 
  101.      * will then call <code>update</code> on the message digest associated
  102.      * with this stream, passing it the byte read. 
  103.      *
  104.      * @return the byte read.
  105.      *
  106.      * @exception IOException if an I/O error occurs.
  107.      * 
  108.      * @see MessageDigest#update(byte) 
  109.      */
  110.     public int read() throws IOException {
  111.     int ch = in.read();
  112.     if (on && ch != -1) {
  113.         digest.update((byte)ch);
  114.     }
  115.     return ch;
  116.     }
  117.  
  118.     /**
  119.      * Reads into a byte array, and updates the message digest (if the
  120.      * digest function is on).  That is, this method reads up to
  121.      * <code>len</code> bytes from the input stream into the array 
  122.      * <code>b</code>, starting at offset <code>off</code>. This method 
  123.      * blocks until the data is actually
  124.      * read. If the digest function is on (see <a href =
  125.      * "#on">on</a>), this method will then call <code>update</code>
  126.      * on the message digest associated with this stream, passing it
  127.      * the data.
  128.      *
  129.      * @param b    the array into which the data is read.
  130.      *
  131.      * @param off the starting offset into <code>b</code> of where the 
  132.      * data should be placed.
  133.      *
  134.      * @param len the maximum number of bytes to be read from the input
  135.      * stream into b, starting at offset <code>off</code>.
  136.      *
  137.      * @return  the actual number of bytes read. This is less than 
  138.      * <code>len</code> if the end of the stream is reached prior to 
  139.      * reading <code>len</code> bytes. -1 is returned if no bytes were 
  140.      * read because the end of the stream had already been reached when 
  141.      * the call was made.
  142.      *
  143.      * @exception IOException if an I/O error occurs.
  144.      * 
  145.      * @see MessageDigest#update(byte[], int, int) 
  146.      */
  147.     public int read(byte[] b, int off, int len) throws IOException {
  148.     int result = in.read(b, off, len);
  149.     if (on && result != -1) {
  150.         digest.update(b, off, result);
  151.     }
  152.     return result;
  153.     }
  154.  
  155.     /**
  156.      * Turns the digest function on or off. The default is on.  When
  157.      * it is on, a call to <a href = "#read">read</a> results in an
  158.      * update on the message digest.  But when it is off, the message
  159.      * digest is not updated.
  160.      *   
  161.      * @param on true to turn the digest function on, false to turn
  162.      * it off.
  163.      */
  164.     public void on(boolean on) {
  165.     this.on = on;
  166.     }
  167.     
  168.     /**
  169.      * Prints a string representation of this digest input stream and
  170.      * its associated message digest object.  
  171.      */
  172.      public String toString() {
  173.      return "[Digest Input Stream] " + digest.toString();
  174.      }
  175. }    
  176.  
  177.  
  178.   
  179.  
  180.